﻿
Public Class rcclient3
    Inherits _rcclient(Of callbackAction, tcpTalk3, fixpoint_talk3pool_t)

    Public Sub New(ByVal host As String, ByVal port As Int32,
                   Optional ByVal holdtimeMs As Int64 = tcpTalk3.defaultHoldTimeMs,
                   Optional ByVal livetimeMs As Int64 = tcpTalk3.defaultLiveTimeMs)
        MyBase.New(host, port, holdtimeMs, livetimeMs)
    End Sub

    Protected Overrides Function async_talk(ByRef action As callbackAction,
                                            ByVal talk As tcpTalk3,
                                            ByVal returnTypes() As Type,
                                            ByVal rtns As pointer(Of fromBytes())) As Boolean
        Dim talkAction As callbackAction = Nothing
        action = New callbackAction(Function() As Boolean
                                        talk.talk(talkAction)
                                        Return True
                                    End Function,
                                    callbackAction.action_check(Function() As callbackAction
                                                                    Return talkAction
                                                                End Function),
                                    Function() As Boolean
                                        Return talk_finish(callbackAction.success_finished(talkAction),
                                                           talk,
                                                           returnTypes,
                                                           rtns)
                                    End Function)
        Return begin(action)
    End Function

    Protected Overrides Function generateTalkPool(ByVal host As String,
                                                  ByVal port As Int32,
                                                  ByVal holdtimeMs As Int64,
                                                  ByVal livetimeMs As Int64) As fixpoint_talk3pool_t
        Return New fixpoint_talk3pool_t(host, port, holdtimeMs, livetimeMs)
    End Function

    Public Shared Sub unittest()
        Dim client As rcclient3 = Nothing
        client = New rcclient3(network.localhost, share.unittest.port)
        Console.WriteLine(longTime())
        Dim rtns As pointer(Of fromBytes()) = Nothing
        rtns = New pointer(Of fromBytes())()
        Dim i As Int64 = 0
        i = -1000
        Dim action As callbackAction = Nothing
        Dim d As _do(Of Boolean) =
            Function() As Boolean
                Return client.invoke(share.unittest.unittestInvokeName, action, rtns, New bytesInt64(i))
            End Function
        Dim action2 As callbackAction =
            New callbackAction(d,
                               callbackAction.rotate_check(
                                   callbackAction.action_sub_check(
                                       Function() As callbackAction
                                           Return action
                                       End Function),
                                   Function() As ternary
                                       If callbackAction.success_finished(action) Then
                                           Console.WriteLine(Convert.ToString(rtns.get()(0)))
                                       End If
                                       If i <= 999 Then
                                           i += 1
                                           If d() Then
                                               Return callbackAction.step_check_pass
                                           End If
                                       End If
                                       Return callbackAction.step_check_finish
                                   End Function))
        assert_begin(action2)
        While Not action2.finished()
            sleep(10)
        End While
        Console.WriteLine(longTime())
    End Sub

    Public Shared Sub performanceTest()
        Dim testsize As Int64 = 10240000
        If isreleasebuild() Then
            testsize *= 20
        End If

        Dim client As rcclient3 = Nothing
        client = New rcclient3(network.localhost, share.unittest.port)
        raiseError("******* automatical remotecall3 perfTest *******", errorHandle.errorType.application)

        Dim rtns As pointer(Of fromBytes()) = Nothing
        rtns = New pointer(Of fromBytes())()
        Dim i As Int64 = 0
        Dim action As callbackAction = Nothing
        Dim d As _do(Of Boolean) =
            Function() As Boolean
                If rnd(0, 2, True) = 0 Then
                    raiseError("remotecall with argument as a guid string.")
                    Return client.invoke(share.unittest.unittestInvokeName, action, rtns, New bytesString(createGUID()))
                Else
                    raiseError("remotecall with argument " + Convert.ToString(i))
                    Return client.invoke(share.unittest.unittestInvokeName, action, rtns, New bytesInt64(i))
                End If
            End Function
        Dim action2 As callbackAction =
                           New callbackAction(d,
                                   callbackAction.rotate_check(
                                       callbackAction.action_sub_check(
                                           Function() As callbackAction
                                               Return action
                                           End Function),
                                           Function() As ternary
                                               If callbackAction.success_finished(action) Then
                                                   raiseError("remotecall returns ", Convert.ToString(rtns.get()(0)))
                                               End If
                                               If i <= testsize Then
                                                   i += 1
                                                   If d() Then
                                                       Return callbackAction.step_check_pass
                                                   End If
                                               End If
                                               Return callbackAction.step_check_finish
                                           End Function))
        assert_begin(action2)
        While Not action2.finished()
            sleep(10)
        End While
        raiseError("********* finish automatical3 perfTest *********", errorHandle.errorType.application)
    End Sub
End Class
